package smlab.media
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import smlab.media.SimpleFLVPlayer;
	import smlab.utils.DisplayObjectUtil;
	import smlab.view.ui.GetUIType;
	import smlab.view.ui.Panel_SM;
	import smlab.view.ui.UIManager_SM;

	/**
	 * flv 播放管理器
	 * 视频文件统一放在 release\video 目录下
	 * @author weiwei_zhang
	 * 
	 */
	public class VideoManager
	{
		private static var instance:VideoManager;
		private var container_:DisplayObjectContainer;
		private var loadPanel:Panel_SM;
		private var playerSkinURL_:String;
		private var playerSkin:MovieClip;//播放器皮肤
		private var closeBtn:SimpleButton;//播放器皮肤
		private var _flvURL:String
		private var _isPlayImmediately:Boolean;
		private var _bufferTime:Number;
		private var _player:SimpleFLVPlayer;
		private var _videoLoadedFun:Function;
		private var _playOverFun:Function;
		private var bgMask:Sprite;//播放视频时的遮罩，挡住防止舞台点击操作
		
		public function VideoManager()
		{
			if (instance != null) {
				return;
			}
		}
		
		public static function getInstance():VideoManager
		{
			if (instance == null) {
				instance = new VideoManager();
			}
			return instance;
		}
		
		/**
		 * 
		 * @param flvURL				视频路径
		 * @param isPlayImmediately		是否立刻开始播放（如果 false 则加载完后播放）
		 * @param playOverFun			播放完毕的时候调用的方法（播放出错或加载出错或者关掉视频也会调用）
		 * @param bufferTime			缓冲时间（尽在 isPlayImmediately = true 时有效）
		 * @param videoLoadedFun		视频加载完毕的时候调用的方法
		 * 
		 */
		public function play(flvURL:String,container:DisplayObjectContainer=null,playerSkinURL:String="",isPlayImmediately:Boolean=false,playOverFun$:Function=null,bufferTime:Number=5,videoLoadedFun$:Function=null):void
		{
			if(loadPanel){
				removeAll();
			}
			
			_flvURL=flvURL;
			_isPlayImmediately=isPlayImmediately;
			_bufferTime=bufferTime;
			_videoLoadedFun=videoLoadedFun$;
			_playOverFun=playOverFun$;
			container_=container;
			init();
		}
		
		private function videoLoadedFun():void
		{
			if(_videoLoadedFun!=null){
				_videoLoadedFun();
				_videoLoadedFun=null
			}
			
			UIManager_SM.getInstance().loadingPanel.hide();
		}
		
		private function playOverFun():void
		{
			if(_playOverFun!=null){
				_playOverFun();
				_playOverFun=null
			}
			
			removeAll();
		}
		
		private function init():void
		{
			loadPanel=new Panel_SM;
			loadPanel.loadUI(null,playerSkinURL_,"SimpleFlvPlayerUI",GetUIType.getDefFromLoaderAppByName,uiloaded,true,true,removeAll,null,false,false,"正在加载视频播放器……");
		}
		
		private function uiloaded():void
		{
			bgMask=new Sprite;
			bgMask.graphics.beginFill(0,0.6);
			bgMask.graphics.drawRect(0,0,800,600);
			bgMask.graphics.endFill();
			container_.addChildAt(bgMask,0);
			
			playerSkin = MovieClip(loadPanel.skin);
			closeBtn=playerSkin.getChildByName("closeBtn") as SimpleButton;
			closeBtn.addEventListener(MouseEvent.CLICK,onClose);
			playerSkin.x=164;
			playerSkin.y=144;
			container_.addChild(playerSkin);
			
			UIManager_SM.getInstance().loadingPanel.setPercent(0,0,"正在加载视频……");
			UIManager_SM.getInstance().loadingPanel.showCloseBtn(false);
			UIManager_SM.getInstance().loadingPanel.hasScreenBg=false;
			UIManager_SM.getInstance().loadingPanel.show();
			
			_player=new SimpleFLVPlayer;
			_player.x=playerSkin.x+27;
			_player.y=playerSkin.y+32;
			_player.videoWidth=420;//704;
			_player.videoHeight=240;//401;
			container_.addChild(_player);
			_player.play(_flvURL,_isPlayImmediately,playOverFun,playStartFun,_bufferTime,videoLoadedFun);
		}
		
		private function onClose(e:MouseEvent):void
		{
			playOverFun();
		}
		
		private function playStartFun():void
		{
//			SystemModel.getinstance().rememberOldSet();
//			SystemModel.getinstance().stopAllSound();
		}
		
		private function removeAll():void
		{
			if(bgMask){
				if(bgMask.parent){
					bgMask.parent.removeChild(bgMask);
				}
				bgMask=null;
			}
			
			UIManager_SM.getInstance().loadingPanel.showCloseBtn(true);
			UIManager_SM.getInstance().loadingPanel.hasScreenBg=true;
			UIManager_SM.getInstance().loadingPanel.hide();
			_player.removeAll();
			DisplayObjectUtil.removeFromParent(_player);
			DisplayObjectUtil.removeFromParent(playerSkin);
			DisplayObjectUtil.removeFromParent(loadPanel);
			loadPanel.removeAll();
			playerSkin=null;
			closeBtn=null;
			_player=null;
			_videoLoadedFun=null;
			_playOverFun=null;
			loadPanel=null;
//			SystemModel.getinstance().resetOldSet();
		}
	}
}